﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions;
using System.Configuration;
using System.IO;
using System.Data;

namespace TCCT_CTC_CSDLDangVienV1
{
    class Util
    {
        public static class DATETIME
        {
            /// <summary>
            /// Toes the date from string VN.
            /// On error return Null
            /// </summary>
            /// <param name="sDateTimeVN">The s date time VN.</param>
            /// <returns></returns>
            public static Nullable<DateTime> ToDateFromStringVN_Nullable(string sDateTimeVN)
            {
                try
                {
                    sDateTimeVN = sDateTimeVN.Replace("Ngày", "").Replace("tháng", "/").Replace("năm", "/");
                    string[] sArr = sDateTimeVN.Split('/');
                    string snam = "";
                    if (sArr[2].Length == 2)
                        snam = "20" + sArr[2];
                    else
                        snam = sArr[2];

                    DateTime rDAte = new DateTime(Util.ToInt(snam), Util.ToInt(sArr[1]), Util.ToInt(sArr[0]));

                    if (rDAte.Year < 1800)
                        return null;

                    return rDAte;
                }
                catch
                {
                    return null;
                }
            }
            /// <summary>
            /// Hàm chuyển đổi chuỗi MM/yyyy sang dạng DateTime, cho ngày là 1
            /// </summary>
            /// <param name="sMMyyyy"></param>
            /// <returns></returns>
            public static Nullable<DateTime> ToDateFromMMyyyy(string sMMyyyy)
            {
                try
                {

                    sMMyyyy = "1/" + sMMyyyy;
                    string[] sArr = sMMyyyy.Split('/');
                    string snam = "";
                    if (sArr[2].Length == 2)
                        snam = "20" + sArr[2];
                    else
                        snam = sArr[2];

                    DateTime rDate = new DateTime(Util.ToInt(snam), Util.ToInt(sArr[1]), Util.ToInt(sArr[0]));

                    if (rDate.Year < 1800) return null;

                    return rDate;
                }
                catch
                {
                    return null;
                }
            }

            /// <summary>
            /// Hàm chuyển đỗi chuỗi vào ngày sinh. Nếu chỉ có năm thì cho 1/7. Nếu chỉ có tháng năm thì cho 1/tháng năm
            /// </summary>
            /// <param name="sNgaySinh"></param>
            /// <returns></returns>
            public static Nullable<DateTime> ParseNgaySinh(string sNgaySinh)
            {
                try
                {
                    int ngay = 1, thang = 7, nam = 0;

                    if (sNgaySinh.Trim() == "") return null;

                    string[] sArr = sNgaySinh.Split('/');
                    string snam = "", sthang = "7", sngay = "1";

                    if (sArr.Length == 1)
                        snam = sArr[0];
                    else if (sArr.Length == 2)
                    {
                        sthang = sArr[0];
                        snam = sArr[1];
                    }
                    else if (sArr.Length == 3)
                    {
                        sngay = sArr[0];
                        sthang = sArr[1];
                        snam = sArr[2];
                    }

                    if (snam.Length == 2)
                        snam = "20" + sArr[2];
                    nam = Util.ToInt(snam);
                    ngay = Util.ToInt(sngay);
                    thang = Util.ToInt(sthang);

                    DateTime rDate = new DateTime(nam, thang, ngay);
                    if (rDate.Year < 1800) return null;
                    return rDate;
                }
                catch
                {
                    return null;
                }
            }
            public static Nullable<DateTime> ToDateFromStringVN(string sDateTimeVN)
            {
                try
                {
                    if (sDateTimeVN.Trim() == "") return null;
                    sDateTimeVN = sDateTimeVN.Replace("Ngày", "").Replace("tháng", "/").Replace("năm", "/");
                    string[] sArr = sDateTimeVN.Split('/');
                    string snam = "";
                    if (sArr[2].Length == 2)
                        snam = "20" + sArr[2];
                    else
                        snam = sArr[2];

                    DateTime rdate = new DateTime(Util.ToInt(snam), Util.ToInt(sArr[1]), Util.ToInt(sArr[0]));
                    if (rdate.Year < 1800)
                        return null;
                    return rdate;
                }
                catch
                {
                    return null;
                }
            }

            public static string To_String(object InputDate)
            {
                try
                {
                    return DateTime.Parse(InputDate.ToString()).ToString(MyConst.FORMAT_DATETIME.ddMMyyyy);
                }
                catch
                {
                    return string.Empty;
                }
            }
            public static void FormatText(System.Windows.Forms.TextBox txt)
            {
                try
                {


                    DateTime temDate = ToDateFromStringVN(txt.Text).Value;
                    if (temDate == null)
                    {
                        txt.Text = txt.Text.Trim();
                        temDate = ToDateFromStringVN(txt.Text.Substring(0, 2) + "/" + txt.Text.Substring(2, 2) + "/" + txt.Text.Substring(4, txt.Text.Length - 4)).Value;
                    }

                    txt.Text = temDate != null ? temDate.ToString(MyConst.FORMAT_DATETIME.ddMMyyyy) : string.Empty;
                }
                catch
                {
                    txt.Text = "";
                }
            }

            /// <summary>
            /// Hàm chuyển sang kiểu datetime với đầu vào dạng datetime lấy từ Database  mà không phụ thuộc định dạng ngày tháng của máy tính
            /// </summary>
            /// <param name="oDate">giá trị datetime của trường datetime trong table</param>
            /// <returns></returns>
            public static Nullable<DateTime> ToDate(object oDate)
            {
                try
                {
                    return (DateTime)oDate;
                }
                catch
                {
                    return null;
                }
            }

        }

        public static class LONG
        {
            public static void FormatText(System.Windows.Forms.TextBox txt)
            {
                try
                {
                    txt.Text = long.Parse(txt.Text.Trim()).ToString();
                }
                catch
                {
                    txt.Text = "";
                }
            }

            public static Nullable<long> ToLongNull(System.Windows.Forms.TextBox txt)
            {
                try
                {
                    return long.Parse(txt.Text.Trim());
                }
                catch
                {
                    return null;
                }
            }
            public static Nullable<long> ToLongNull(string sInput)
            {
                try
                {
                    return long.Parse(sInput);
                }
                catch
                {
                    return null;
                }
            }
            public static long ToLong(object sInput)
            {
                try
                {
                    return long.Parse(sInput.ToString());
                }
                catch
                {
                    return 0;
                }
            }
        }

        public static class HOTEN
        {
            public static string Ho(string hoTen)
            {
                try
                {
                    hoTen = hoTen.Trim();
                    int i = hoTen.IndexOf(" ");
                    return hoTen.Substring(0, i).Trim();
                }
                catch
                {
                    return "";
                }
            }
            public static string Dem(string hoTen)
            {
                try
                {
                    hoTen = hoTen.Trim();
                    int i = hoTen.IndexOf(" ");
                    int j = hoTen.LastIndexOf(" ");
                    return hoTen.Substring(i, j - i).Trim();
                }
                catch
                {
                    return "";
                }
            }
            public static string Ten(string hoTen)
            {
                try
                {
                    hoTen = hoTen.Trim();
                    int i = hoTen.LastIndexOf(" ");
                    return hoTen.Substring(i, hoTen.Length - i).Trim();
                }
                catch
                {
                    return "";
                }
            }
        }
        public static class COMBOBOX
        {
            public static void SetValue(System.Windows.Forms.ComboBox cbo, object objValue)
            {
                try
                {
                    cbo.SelectedValue = objValue;
                }
                catch
                {
                    cbo.SelectedIndex = -1;
                }
            }
            public static object GetValue(System.Windows.Forms.ComboBox cbo)
            {
                try
                {
                    object resul = null;
                    if (cbo.SelectedValue.ToString() != "0")
                        resul = cbo.SelectedValue;
                    return resul;
                }
                catch
                {
                    return null;
                }
            }
        }
        public static class REPORT
        {
            public static void RenderReport(CrystalDecisions.Windows.Forms.CrystalReportViewer objCrystalRptVw, string ReportName, DataTable MyDataTable)
            {
                CrystalDecisions.CrystalReports.Engine.ReportDocument objReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                objReport.Load(System.Windows.Forms.Application.StartupPath + "\\Reports\\" + ReportName);
                RenderReport(objCrystalRptVw, MyDataTable, objReport);
            }

            /// <summary>
            /// Hàm show report truyền vào một bảng
            /// </summary>
            /// <param name="objClsCrystalRptVw"></param>
            /// <param name="MyDataTable"></param>
            /// <param name="objReport"></param>
            public static void RenderReport(CrystalDecisions.Windows.Forms.CrystalReportViewer objCrystalRptVw, DataTable MyDataTable, CrystalDecisions.CrystalReports.Engine.ReportDocument objReport)
            {
                objReport.SetDataSource(MyDataTable);
                objCrystalRptVw.ReportSource = objReport;
            }
            public static void RenderReport(CrystalDecisions.Windows.Forms.CrystalReportViewer objCrystalRptVw, string ReportName)
            {
                CrystalDecisions.CrystalReports.Engine.ReportDocument objReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                objReport.Load(System.Windows.Forms.Application.StartupPath + "\\Reports\\" + ReportName);
                RenderReport(objCrystalRptVw, objReport);
            }

            public static void RenderReport(CrystalDecisions.Windows.Forms.CrystalReportViewer objClsCrystalRptVw, CrystalDecisions.CrystalReports.Engine.ReportDocument objReport)
            {
                objClsCrystalRptVw.ReportSource = objReport;
            }
        }

        public static bool ToBool(object objValue)
        {
            if (objValue == null || objValue.ToString() == "" || objValue.ToString() == "0" || objValue.ToString().ToLower() == "false")
                return false;

            return true;
        }

        /// <summary>
        /// Default return 0
        /// </summary>
        /// <param name="objValue"></param>
        /// <returns></returns>
        public static double ToDouble(object objValue)
        {
            try
            {
                return Convert.ToDouble(objValue);
            }
            catch
            {
                return 0;
            }
        }

        public static Nullable<int> ToIntNull(object objValue)
        {
            try
            {
                return Convert.ToInt32(objValue);
            }
            catch
            {
                return null;
            }
        }


        public static int ToInt(object objValue)
        {
            try
            {
                return Convert.ToInt32(objValue);
            }
            catch
            {
                return 0;
            }
        }

        public static void ThongBao(object obj)
        {
            System.Windows.Forms.MessageBox.Show(obj.ToString(), "Thong Bao", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
        }
        public static void ThongBaoLoi(object obj)
        {
            System.Windows.Forms.MessageBox.Show(obj.ToString(), "Thong Bao", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
        }
    }
}
